<?php

/**
 * BH造型单表model
 * @author perry
 */

namespace Home\Model;
// use Think\Model\ViewModel;
use Think\Model;

class BHClosetModel extends Model {
	private $closetAlias='cc';
	private $favAlias="fav";

	 /**
     * 初始化
     */
	// function _initialize(){
	// 	// $this->tableName = 'bh_closet_commodity';
 //        // $this->trueTableName  = 'eosi_bh_looks';
 //        $this->imgUploadType='closet';
 //        $this->commodityImgUploadType='commodity';
	// 	$this->viewFields = array(     
 //    		$this->closetAlias=>array('id','uid','type','img','create_time','update_time','status','memo','name','sub_imgs','no','price','dh_date'
 //    			,'_type'=>'LEFT'
 //    			,'_table'=>'eosi_bh_closet_commodity'),     
 //    		$this->favAlias=>array('id'=>'fav_id','cc_id'
 //    			, '_on'=>"{$this->closetAlias}.id={$this->favAlias}.cc_id"
 //    			,'_table'=>'eosi_bh_closet_commodity_favorite'),      
 //    	); 
 //    	$this->looksM=D('Home/BHLooks');
	// }

    /**
     * 初始化
     */
    function _initialize() {
        $this->tableName = 'bh_closet_commodity';
        // $this->trueTableName  = 'eosi_bh_looks';
        $this->imgUploadType='closet';
        $this->commodityImgUploadType='commodity';
    }
    
    
    function getByPage($uid,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true,$id=-1,$order="default"){
    	if($order=='favorite'){
    		$order='eosi_bh_closet_commodity_favorite.id desc, eosi_bh_closet_commodity.update_time desc';
    	}
    	else {
    		$order='eosi_bh_closet_commodity.update_time desc';
    	}
    	$map=array();
    	if(ic_isPosInt($uid)){
    		//这是获得前台用户的衣橱数据
	    	$map['eosi_bh_closet_commodity.uid']=$uid;
	    	
	    }
	    elseif (is_array($uid)) {
	    	//这是获得商品数据
	    	// $super_style_uid_str=implode(",", $uid);
	    	// $map['_string'] = " uid in ($super_style_uid_str) ";
	    	$map['eosi_bh_closet_commodity.uid'] = array('in', $uid);
	    	
	    }

	    if(ic_isPosInt($type)){
	    	if($type!=1){ //type==1 代表全部类型，无需筛选
		    	$map['type']=$type;
		    }
	    }
	    if(ic_isPosInt($status)){
		    	$map['status']=$status;
	    }
	    if($name!==false){
	    	$namef=ic_filterStr($name);
	    		if(ic_checkUnEmpty($namef)){
			    	// $map['name']=array('exp'," LIKE (1,3,8) ");
		    		$map['name'] = array('like',"%$name%");
		    	}
	    }
	    if(!ic_isPosInt($page)){
	    	$page=1;
	    }
	    if(!ic_isPosInt($pageSize)){
	    	$pageSize=10;
	    }
	    if($pageSize>50000){
	    	$pageSize=50000;
	    }
	    if(ic_isPosInt($id)){
	    	$map['eosi_bh_closet_commodity.id']=$id;
	    }
	    elseif (is_array($id)) {
	    	$map['eosi_bh_closet_commodity.id'] = array("in", $id);
	    }
	    elseif ($id!==-1) {
	    	$id_arr=explode(",", $id);
	    	$real_id_arr=array();
	    	foreach ($id_arr as $key => $iid) {
	    		if(ic_isPosInt($iid)){
	    			$real_id_arr[]=$iid;
	    		}
	    	}
	    	if(count($real_id_arr)>0){
	    		$map['eosi_bh_closet_commodity.id'] = array("in", $real_id_arr);
	    	}
	    }
	    $totalCount=$this->where($map)->count();
	    $debug_sql=$this->getLastSql();

	    $datas=array();

	    if(($totalCount>($page-1)*$pageSize)){
	    	$start= ($page-1)*$pageSize;
	    	if($field!==true){
	    		$field.=",create_time";
	    	}
	    	$ab ='eosi_bh_closet_commodity_favorite.id as fav_id ,eosi_bh_closet_commodity.*';
	    	$datas=$this->join(' left join eosi_bh_closet_commodity_favorite ON eosi_bh_closet_commodity_favorite.cc_id = eosi_bh_closet_commodity.id')
	    	->where($map)->order($order)->field($ab)->limit($start,$pageSize)->select();
	    	$adminArray=C('super_style_uid');
	    	foreach ($datas as $key => &$value) {
    			if(in_array($value['uid'],$adminArray )){
    				$imgsubType=$this->commodityImgUploadType;
    			}
    			else {
    				$imgsubType=$this->imgUploadType;
    			}
	    		//如果返回数据里有主图（商品和衣橱都有字段img），需要转换成图片全路径
	    		if(array_key_exists('img', $value)){
		    		$value['img']=ic_getUploadImgFullPath($imgsubType,$value['img'],$value['create_time']);
		    	}
		    	//如果返回数据里有副图（商品特有字段sub_imgs），需要转换成图片全路径
		    	if(array_key_exists('sub_imgs', $value)){
		    		if(ic_checkUnEmpty($value['sub_imgs'])){
			    		$tmparr=explode(',',$value['sub_imgs']);
				        foreach ($tmparr as $key2 => $value2) {
				            $tmparr[$key2] = ic_getUploadImgFullPath($imgsubType,$value2,$value['create_time']);
				        }
			    		$value['sub_imgs'] = $tmparr;
			    	}
			    	else {
			    		$value['sub_imgs'] = array();
			    	}
		    	}
		    	if(array_key_exists('dh_date', $value)){
		    		$value['dh_date']=ic_dateToStr($value['dh_date'],'Y.m.d');
		    	}
		    	if(array_key_exists('create_time', $value)){
		    		$value['create_time']=ic_dateToStr($value['create_time'],'Y.m.d');
		    	}

	    	}
	    }
	    // dump($this->getLastSql());
	    $totalPage=floor($totalCount/$pageSize)+((($totalCount%$pageSize)==0)?0:1);
	    
	    return array('totalCount'=>$totalCount,'totalPage'=>$totalPage, 'pageList'=>$datas);
    }

    function deleteOne($uid,$id){
    	if(!ic_isPosInt($id)){
	    	throw new \Exception("id参数有误.", -73);
	    }
    	$map=array('id'=>$id);
    	$exist=$this->where($map)->find();
    	if(!$exist){
    		throw new \Exception("待删除的数据不存在哦,请刷新页面重试.", -78);
    	}
    	if($exist['uid']!=$uid){
    		throw new \Exception("您无权限删除他人的数据，请检查.", -81);
    	}
    	//先删数据
    	$deleteRe=$this->where($map)->delete();
    	//再删物理图片
    	$oldfilePath=ic_getUploadImgServerPath('closet',$exist['create_time']);
        $this->delMulti($oldfilePath,$exist['img']);
        $tmparr=explode(',',$exist['sub_imgs']);
        foreach ($tmparr as $key => $value) {
            $this->delMulti($oldfilePath,$value);
        }
        return $deleteRe;
    }

    private function delMulti($serverPath,$delName){
        @unlink($serverPath.$delName);
        @unlink($serverPath."m_".$delName);
        @unlink($serverPath."s_".$delName);
    }
    

}
